package com.atldy.commerce.controller;

import cn.hutool.json.JSONUtil;
import com.atldy.commerce.annotation.IgnoreResponseAdvice;
import com.atldy.commerce.service.JwtService;
import com.atldy.commerce.vo.JwtToken;
import com.atldy.commerce.vo.UsernameAndPassword;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0
 * @Author: lidongyang
 * @CreateTime: 2021-08-02 21:50
 * @Description： 授权服务暴露出去的接口
 */
@Slf4j
@RestController
@RequestMapping("/authority")
public class JwtController {

    @Autowired
    private JwtService jwtService;


    /**
     * @param usernameAndPassword
     * @return 认证授权，登录发放token
     */
    @IgnoreResponseAdvice
    @PostMapping("/token")
    public JwtToken login(@RequestBody UsernameAndPassword usernameAndPassword) throws Exception {
        log.info("登录用户信息是：[{}]", JSONUtil.toJsonPrettyStr(usernameAndPassword));
        return new JwtToken(
                jwtService.generatorToken(usernameAndPassword.getUsername(),
                        usernameAndPassword.getPassword())
        );
    }

    @IgnoreResponseAdvice
    @PostMapping("/register")
    public JwtToken register(@RequestBody UsernameAndPassword usernameAndPassword) throws Exception {
        log.info("注册用户信息是：[{}]", JSONUtil.toJsonPrettyStr(usernameAndPassword));
        return new JwtToken(jwtService.registerAndToken(usernameAndPassword));

    }


}